var express = require('express');
var router = express.Router();

/* GET home page. */
// router.get('/', function (req, res, next) {
//     res.render('index', {
//         title: 'Express'
//     });
// });

//引入配置和数据库操作
const {
    API_URL
} = require('../utils/config')
const Query = require('../utils/db')

//接口  首页-banner接口
router.get('/api/banners', async (req, res, next) => {
    let {
        err,
        result
    } = await Query(`select id,title,concat('${API_URL}/uploads/banners/',image_src) as image_src from e_banners where isshow = 1`)
    if (err) {
        next({
            msg: "请稍后再试",
        })
        return;
    }
    if (result.length == 0) {
        next({
            msg: "无数据",
        })
        return;
    }
    next({
        status: 200,
        result,
    })
})

//首页-课程列表接口
router.get('/api/courseinfo', async (req, res, next) => {
    let {
        type = 1
    } = req.query;
    type == 1 ? limit = 8 : limit = 6

    let {
        err,
        result
    } = await Query(`select id,cid,concat('${API_URL}/uploads/banners/',image_src) as image_src,price,type_name,area_name,subject_name,type,title from e_course where type = ${type} AND status = 1 limit ${limit}`)

    if (err) {
        next({
            msg: "请稍后再试",
        })
        return;
    }
    if (result.length == 0) {
        next({
            msg: "无数据",
        })
        return;
    }
    next({
        status: 200,
        result,
    })
})

module.exports = router;